﻿using HardcoreGame.Common.Helpers;
using HardcoreGame.Content.Items.Weapons.Ranged.DoubleGun;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.IO;
using Terraria;
using Terraria.Audio;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ranged
{
    public class OnyxDoubleGun2 : ModProjectile
    {
        public bool shooted;
        public int animationTimerMax;
        public int animationTimer = 6;
        private enum Mode
        {
            Shoot,
            NoShoot,
            Channel,
            Reload
        }
        public override void SendExtraAI(BinaryWriter writer)
        {
            writer.Write(shooted);
            writer.Write(animationTimerMax);
            writer.Write(animationTimer);
        }
        public override void ReceiveExtraAI(BinaryReader reader)
        {
            shooted = reader.ReadBoolean();
            animationTimerMax = reader.ReadInt32();
            animationTimer = reader.ReadInt32();
        }
        public override bool? CanDamage()
        {
            return new bool?(false);
        }
        public override bool ShouldUpdatePosition()
        {
            return false;
        }
        public override void SetDefaults()
        {
            Projectile.width = 24;
            Projectile.height = 96;
            Projectile.friendly = true;
            Projectile.penetrate = -1;
            Projectile.tileCollide = false;
            Projectile.ignoreWater = true;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.timeLeft = 999999;
        }
        public override void AI()
        {
            Player player = Main.player[Projectile.owner];
            Vector2 relativeRot = player.RotatedRelativePoint(player.MountedCenter, true, true);
            bool flag = animationTimerMax == 0;
            if (flag)
            {
                animationTimerMax = animationTimer;
            }
            bool flag2 = player.HeldItem.type != ModContent.ItemType<OnyxDoubleGun>() || !player.active || player.dead;
            if (flag2)
            {
                Projectile.Kill();
            }
            else
            {
                bool flag3 = Main.myPlayer == Projectile.owner;
                if (flag3)
                {
                    float speed = player.inventory[player.selectedItem].shootSpeed;
                    Vector2 rRot = relativeRot;
                    Vector2 rot = Main.screenPosition + new Vector2(Main.mouseX, Main.mouseY) - rRot;
                    bool flag4 = player.gravDir == -1f;
                    if (flag4)
                    {
                        rot.Y = Main.screenHeight - Main.mouseY + Main.screenPosition.Y - rRot.Y;
                    }
                    Vector2 vel = Vector2.Normalize(rot);
                    bool flag5 = float.IsNaN(vel.X) || float.IsNaN(vel.Y);
                    if (flag5)
                    {
                        vel = -Vector2.UnitY;
                    }
                    vel = Vector2.Normalize(Vector2.Lerp(vel, Vector2.Normalize(Projectile.velocity), 0.9f));
                    vel *= speed;
                    bool flag6 = vel.X != Projectile.velocity.X || vel.Y != Projectile.velocity.Y;
                    if (flag6)
                    {
                        Projectile.netUpdate = true;
                    }
                    Projectile.velocity = vel;
                    bool flag7 = Projectile.ai[0] == 0f;
                    if (flag7)
                    {
                        bool flag8 = animationTimer > 0;
                        if (flag8)
                        {
                            animationTimer--;
                        }
                        bool flag9 = !shooted;
                        if (flag9)
                        {
                            float rota = Projectile.velocity.ToRotation();
                            float num = 15f;
                            int direction = Projectile.direction;
                            Vector2 pp = new Vector2(num, 0).RotatedBy((double)rota, default);
                            animationTimer = animationTimerMax;
                            Projectile proj = Projectile.NewProjectileDirect(null, Projectile.Center + pp, vel, ModContent.ProjectileType<OnyxShot>(), Projectile.damage, Projectile.knockBack, Projectile.owner, 0f, 0f, 0f);
                            int penetrate = player.itemAnimationMax < 8 ? 1 : 3;
                            proj.penetrate = penetrate;
                            SoundStyle soundStyle = SoundID.Item36;
                            soundStyle = soundStyle.WithVolumeScale(0.75f);
                            SoundEngine.PlaySound(soundStyle, new Vector2?(Projectile.position), null);
                            shooted = true;
                            animationTimer = animationTimerMax;
                        }
                        else
                        {
                            bool flag15 = Projectile.timeLeft > player.itemAnimationMax;
                            if (flag15)
                            {
                                Projectile.timeLeft = player.itemAnimationMax;
                            }
                        }
                    }
                    else
                    {
                        bool flag16 = Projectile.ai[0] == 1f;
                        if (flag16)
                        {
                            bool flag17 = Projectile.timeLeft > player.itemAnimationMax;
                            if (flag17)
                            {
                                Projectile.timeLeft = player.itemAnimationMax;
                            }
                        }
                        else
                        {
                            bool flag18 = Projectile.ai[0] == 3f;
                            if (flag18)
                            {
                                bool flag19 = animationTimer > 0;
                                if (flag19)
                                {
                                    animationTimer--;
                                }
                                bool flag20 = Projectile.timeLeft > 60;
                                if (flag20)
                                {
                                    Projectile.timeLeft = 60;
                                }
                            }
                        }
                    }
                }
                float offset = MyUtils.QuadraticEase(animationTimer, 0f, animationTimerMax, -0.25f, 0f, false, 0.7f);
                bool flag21 = Projectile.ai[0] == 3f;
                if (flag21)
                {
                    offset = MyUtils.Map(animationTimer, 0, animationTimerMax, 0f, 6.2831855f);
                }
                Projectile.rotation = Projectile.velocity.ToRotation() + 1.5707964f + offset * Projectile.direction;
                Projectile.position = player.RotatedRelativePoint(player.MountedCenter, true, true) - Projectile.Size / 2f - new Vector2(0f, 25f).RotatedBy(Projectile.rotation, default);
                Projectile.spriteDirection = Projectile.direction;
                player.ChangeDir(Projectile.direction);
                player.itemRotation = (float)Math.Atan2((double)(Projectile.velocity.Y * Projectile.direction), (double)(Projectile.velocity.X * Projectile.direction));
                int[] array = new int[4];
                array[1] = 3;
                array[2] = 2;
                int stretch = 0;
                player.SetCompositeArmBack(true, (Player.CompositeArmStretchAmount)stretch, player.itemRotation - 1.5707964f * Projectile.direction);
            }
        }
        public override bool PreDraw(ref Color lightColor)
        {
            Player player = Main.player[Projectile.owner];
            Texture2D tex = TextureAssets.Projectile[Type].Value;
            SpriteEffects sprite = (SpriteEffects)(Projectile.spriteDirection == -1 ? 1 : 0);
            Main.spriteBatch.Draw(tex, Projectile.Center - Main.screenPosition, null, lightColor, Projectile.rotation, new Vector2(tex.Width, tex.Height) / 2f, 0.8f, sprite, 0f);
            Main.spriteBatch.End();
            Main.spriteBatch.Begin((SpriteSortMode)1, BlendState.NonPremultiplied, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, null, Main.GameViewMatrix.TransformationMatrix);
            return false;
        }
        public override void PostDraw(Color lightColor)
        {
            Main.spriteBatch.End();
            Main.spriteBatch.Begin(0, BlendState.NonPremultiplied, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, null, Main.GameViewMatrix.TransformationMatrix);
            base.PostDraw(lightColor);
        }
    }
}
